#!/bin/tcsh -ef
#############################################################################
#                                                                           #
# Title: Export Image & MetaData                                            #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 23/06/2017                                             #
# Last Modification: 23/06/2017                                             #
# Author...........: focus-em.org                                           #
#                                                                           #
#############################################################################
#
# SORTORDER: 80 
#
# MANUAL: This script will rsync the selected image and metadata to the export directory. 
# MANUAL: Please note that this will eliminate the possibility to prune the data in the central library table, to export then only the remaining, good images. 
# MANUAL: But exporting during data collection will save you time later, if you want to export everything.:wq
#
# DISPLAY: export_basedir
# DISPLAY: export_rawstack_doit
# DISPLAY: export_rawstack_subdir
# DISPLAY: export_gainref_doit
# DISPLAY: export_gainref_subdir
# DISPLAY: export_pixeldef_doit
# DISPLAY: export_gaincorstack_doit
# DISPLAY: export_gaincorstack_subdir
# DISPLAY: export_driftcorstack_doit
# DISPLAY: export_driftcorstack_subdir
# DISPLAY: export_driftcoraver_doit
# DISPLAY: export_driftcoraver_subdir
# DISPLAY: export_driftcorfig_doit
# DISPLAY: export_driftcorfig_subdir
# DISPLAY: export_CTFDiag_doit
# DISPLAY: export_CTFDiag_subdir
# DISPLAY: export_CTFstar_doit
# DISPLAY: export_CTFstar_subdir
# DISPLAY: export_already_done
# DISPLAY: export_limit_defocus_upper
# DISPLAY: export_limit_defocus_lower
# DISPLAY: defocus_defocus
# DISPLAY: export_limit_ctfres_upper
# DISPLAY: defocus_RESMAX
# DISPLAY: export_limit_drift_upper
# DISPLAY: export_limit_drift_lower
# DISPLAY: import_drift
# DISPLAY: export_limit_iciness_upper
# DISPLAY: iciness
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
#
set comment = ""
set export_basedir = ""
set export_rawstack_doit = ""
set export_rawstack_subdir = ""
set export_gainref_doit = ""
set export_gainref_subdir = ""
set export_pixeldef_doit = ""
set export_gaincorstack_doit = ""
set export_gaincorstack_subdir = ""
set export_driftcorstack_doit = ""
set export_driftcorstack_subdir = ""
set export_driftcoraver_doit = ""
set export_driftcoraver_subdir = ""
set export_driftcorfig_doit = ""
set export_driftcorfig_subdir = ""
set export_CTFDiag_doit = ""
set export_CTFDiag_subdir = ""
set export_CTFstar_doit = ""
set export_already_done = ""
set import_rawstack = ""
set import_gainref = ""
set import_defects = ""
set raw_gaincorrectedstack = ""
set movie_stackname = ""
set phacon = ""
set import_rawstack = ""
set import_gainref = ""
set import_defects = ""
set raw_gaincorrectedstack = ""
set movie_stackname = ""
set KV = ""
set CS = ""
set stepdigitizer = ""
set iciness = ""
set sample_pixel = ""
set defocus = ""
set defocus_CCvalue = ""
set defocus_RESMAX = ""
set defocus_phase_shift = ""
set export_limit_defocus_upper = ""
set export_limit_defocus_lower = ""
set defocus_defocus = ""
set export_limit_ctfres_upper = ""
set defocus_RESMAX = ""
set export_limit_drift_upper = ""
set export_limit_drift_lower = ""
set import_drift = ""
set export_limit_iciness_upper = ""
set iciness = ""
#
#$end_vars
#
set scriptname = export_image
#
\rm -f LOGS/${scriptname}.results
source ${proc_2dx}/initialize
#
set olddir = $PWD
#
set AmpCon = ` echo "scale=3; sqrt( 1 - ${phacon} * ${phacon} )" | bc `
set magnification = `echo "scale=3; ${stepdigitizer} * 10000.0 / ${sample_pixel}" | bc`
#
set defocus_nocomma = `echo ${defocus} | sed 's/,/ /g'`
#
if ( ${export_already_done} == "y" ) then
  #
  ${proc_2dx}/protest "Image already exported. Skipping."
  #
else
  #
  set not_export = 0
  #
  set test1 = `echo ${defocus_defocus} ${export_limit_defocus_upper} | awk '{ if ( $1 > $2 ) { s = 1 } else { s = 0 } } END { print s }'`
  if ( ${test1} == "1" ) then
    ${proc_2dx}/linblock "Not exporting: Defocus is too high."
    set not_export = 1
  endif
  set test1 = `echo ${defocus_defocus} ${export_limit_defocus_lower} | awk '{ if ( $1 < $2 ) { s = 1 } else { s = 0 } } END { print s }'`
  if ( ${test1} == "1" ) then
    ${proc_2dx}/linblock "Not exporting: Defocus is too low."
    set not_export = 1
  endif
  set test1 = `echo ${defocus_RESMAX} ${export_limit_ctfres_upper} | awk '{ if ( $1 > $2 ) { s = 1 } else { s = 0 } } END { print s }'`
  if ( ${test1} == "1" ) then
    ${proc_2dx}/linblock "Not exporting: CTF resolution is too bad."
    set not_export = 1
  endif
  set test1 = `echo ${import_drift} ${export_limit_drift_upper} | awk '{ if ( $1 > $2 ) { s = 1 } else { s = 0 } } END { print s }'`
  if ( ${test1} == "1" ) then
    ${proc_2dx}/linblock "Not exporting: Drift is too high."
    set not_export = 1
  endif
  set test1 = `echo ${import_drift} ${export_limit_drift_lower} | awk '{ if ( $1 < $2 ) { s = 1 } else { s = 0 } } END { print s }'`
  if ( ${test1} == "1" ) then
    ${proc_2dx}/linblock "Not exporting: Drift is too low."
    set not_export = 1
  endif
  set test1 = `echo ${iciness} ${export_limit_iciness_upper} | awk '{ if ( $1 > $2 ) { s = 1 } else { s = 0 } } END { print s }'`
  if ( ${test1} == "1" ) then
    ${proc_2dx}/linblock "Not exporting: Iciness is too high."
    set not_export = 1
  endif
  #    
  if ( ${not_export} == 0 ) then
    cd ${olddir}
    if ( ! -d ${export_basedir} ) then
      ${proc_2dx}/linblock "WARNING: ${export_basedir} not existing. Creating it."
      \mkdir ${export_basedir} 
    endif
    cd ${export_basedir}
    set star_dir = $PWD
    echo "::Export Directory for STAR files is ${star_dir}"
    cd ${olddir}
    #
    echo "<<@progress: 10>>"
    #
    cd ${olddir}
    #
    # Only allow "rsync" to target. This will duplicate the required HD size, but allow full functionality of FOCUS, 
    # including re-processing of images, or pruning and re-exporting of a clean version.
    #
    set local_doit = 1
    #
    set oriname = `echo ${import_rawstack} | sed 's/\./\_/g' | sed 's/_mrc//g' | sed 's/_dm4//g' | sed 's/_tif//g' `
    #
    # set sub_export_anything_doit = "${1}"
    # set sub_doit = "${2}"
    # set sub_basedir = "${3}"
    # set sub_targetdir = "${4}"
    # set sub_filename = "${5}"
    # set sub_targetname = "${6}"
    # 
    # \rsync -auvP ${sub_filename} ${sub_basedir}/${sub_targetdir}/${sub_targetname}
    # 
    source ${proc_2dx}/import_export_sub.com ${local_doit} ${export_rawstack_doit} ${export_basedir} ${export_rawstack_subdir} ${import_rawstack} ${import_rawstack}
    #
    set defects_name = `echo ${import_defects} | rev | cut -d\/ -f1 | rev`
    set gainref_name  = `echo ${import_gainref}  | rev | cut -d\/ -f1 | rev`
    source ${proc_2dx}/import_export_sub.com ${local_doit} ${export_pixeldef_doit} ${export_basedir} ${export_gainref_subdir} ${import_defects} ${defects_name}
    source ${proc_2dx}/import_export_sub.com ${local_doit} ${export_gainref_doit}  ${export_basedir} ${export_gainref_subdir} ${import_gainref} ${gainref_name}
    #
    source ${proc_2dx}/import_export_sub.com ${local_doit} ${export_gaincorstack_doit} ${export_basedir} ${export_gaincorstack_subdir} ${raw_gaincorrectedstack}.mrcs ${oriname}_${raw_gaincorrectedstack}.mrcs
    #
    source ${proc_2dx}/import_export_sub.com ${local_doit} ${export_driftcorstack_doit} ${export_basedir} ${export_driftcorstack_subdir} ${movie_stackname}.mrcs ${oriname}_aligned.mrcs
    #
    source ${proc_2dx}/import_export_sub.com ${local_doit} ${export_driftcoraver_doit} ${export_basedir} ${export_driftcoraver_subdir} ${movie_stackname}.mrc ${oriname}.mrc
    source ${proc_2dx}/import_export_sub.com ${local_doit} ${export_driftcoraver_doit} ${export_basedir} ${export_driftcoraver_subdir} ${movie_stackname}_Sum.mrc ${oriname}_noDW.mrc
    source ${proc_2dx}/import_export_sub.com ${local_doit} ${export_driftcorfig_doit} ${export_basedir} ${export_driftcorfig_subdir} translations.png ${oriname}_translations.png
    source ${proc_2dx}/import_export_sub.com ${local_doit} ${export_driftcorfig_doit} ${export_basedir} ${export_driftcorfig_subdir} 2dx_image.cfg ${oriname}_2dx_image.cfg
    if ( -e motioncor2_shifts.txt ) then
      source ${proc_2dx}/import_export_sub.com ${local_doit} ${export_driftcorfig_doit} ${export_basedir} ${export_driftcorfig_subdir} motioncor2_shifts.txt ${oriname}_motioncor2_shifts.txt
    endif
    #
    source ${proc_2dx}/import_export_sub.com ${local_doit} ${export_CTFDiag_doit} ${export_basedir} ${export_CTFDiag_subdir} CTFDiag.mrc ${oriname}.ctf:mrc
    # 
    if ( ${export_CTFstar_doit} == "y" ) then
      if ( ! -e ${export_basedir}/micrographs_all_defocus.star ) then  
        cat > ${export_basedir}/micrographs_all_defocus.star << eot

data_

loop_
_rlnMicrographName #1
_rlnCtfImage #2
_rlnDefocusU #3
_rlnDefocusV #4
_rlnDefocusAngle #5
_rlnVoltage #6
_rlnSphericalAberration #7
_rlnAmplitudeContrast #8
_rlnMagnification #9
_rlnDetectorPixelSize #10
_rlnCtfFigureOfMerit #11
_rlnCtfMaxResolution #12
_rlnPhaseShift #13
eot
        cat > ${sub_basedir}/micrographs_all_defocus_withIciness.star << eot

data_

loop_
_rlnMicrographName #1
_rlnCtfImage #2
_rlnDefocusU #3
_rlnDefocusV #4
_rlnDefocusAngle #5
_rlnVoltage #6
_rlnSphericalAberration #7
_rlnAmplitudeContrast #8
_rlnMagnification #9
_rlnDetectorPixelSize #10
_rlnCtfFigureOfMerit #11
_rlnCtfMaxResolution #12
_rlnPhaseShift #13
_rlnIciness #14
eot
      endif
      echo "${export_driftcoraver_subdir}/${oriname}.mrc ${export_CTFDiag_subdir}/${oriname}.ctf:mrc" ${defocus_nocomma} \
             ${KV} ${CS} ${AmpCon} ${magnification} ${stepdigitizer} ${defocus_CCvalue} ${defocus_RESMAX} ${defocus_phase_shift}            >> ${export_basedir}/micrographs_all_defocus.star
      echo "${export_driftcoraver_subdir}/${oriname}.mrc ${export_CTFDiag_subdir}/${oriname}.ctf:mrc" ${defocus} \
             ${KV} ${CS} ${AmpCon} ${magnification} ${stepdigitizer} ${defocus_CCvalue} ${defocus_RESMAX} ${defocus_phase_shift} ${iciness} >> ${export_basedir}/micrographs_all_defocus_withIciness.star
    else
      # Use output of gCTF
      if ( ! -e ${export_basedir}/micrographs_all_defocus.star ) then  
        head -n 16 micrographs_all_.ctf:mrc.star > ${export_basedir}/micrographs_all_defocus.star
        head -n 16 micrographs_all_.ctf:mrc.star > ${export_basedir}/micrographs_all_defocus_withIciness.star
        echo "_rlnIciness #13" >> ${export_basedir}/micrographs_all_.ctf:mrc_withIciness.star
      endif
      echo "${export_driftcoraver_subdir}/${oriname}.mrc ${export_CTFDiag_subdir}/${oriname}.ctf:mrc" `tail -n 2 micrographs_all_.ctf:mrc.star | head -n 1 | cut -d\  -f3-`            >> ${export_basedir}/mic  rographs_all_defocus.star
      echo "${export_driftcoraver_subdir}/${oriname}.mrc ${export_CTFDiag_subdir}/${oriname}.ctf:mrc" `tail -n 2 micrographs_all_.ctf:mrc.star | head -n 1 | cut -d\  -f3-` ${iciness} >> ${export_basedir}/micrographs_all_defocus_withIciness.star
      #
      if ( -e diagnostic_output.txt ) then
        # Use output of CTFFIND4
        \rsync -auvP diagnostic_output.txt ${export_basedir}/${export_CTFDiag_subdir}/${oriname}_diagnostic_output.txt
        \rsync -auvP diagnostic_output_avrot.txt ${export_basedir}/${export_CTFDiag_subdir}/${oriname}_diagnostic_output_avrot.txt
      endif
      if ( -e ${movie_stackname}_Sum_.ctf:mrc.log ) then
        # Use output of gCTF
        cat ${movie_stackname}_Sum_.ctf:mrc.log | sed s/${movie_stackname}_Sum/${oriname}/g > ${oriname}.ctf:mrcfind3.log
        source ${proc_2dx}/import_export_sub.com ${local_doit} ${export_CTFstar_doit} ${export_basedir} ${export_CTFDiag_subdir} ${oriname}.ctf:mrcfind3.log ${oriname}.ctf:mrcfind3.log
      endif
      #
      echo "::   Line added to Star file ${export_basedir}/micrographs_all_defocus.star" 
      echo ":: "
      echo "#IMAGE-IMPORTANT: ${export_basedir}/micrographs_all_defocus.star <Star file>" >> LOGS/${scriptname}.results
      echo "#IMAGE-IMPORTANT: ${export_basedir}/micrographs_all_defocus_withIciness.star <Star file w/ iciness>" >> LOGS/${scriptname}.results
      #
    endif
    set export_already_done = "y"
    echo "set export_already_done = ${export_already_done}" >> LOGS/${scriptname}.results
    #
    echo ":: "
    echo "::Exported to:"
    echo "::${export_basedir}"
    echo ":: "
  endif
  #
else
  #
  echo ":: Not Running. "
  #
endif
#
echo "<<@progress: 100>>"
echo "<<@evaluate>>"
#
##########################################################################
${proc_2dx}/linblock "${scriptname} - normal end."
##########################################################################
##
#
#
#
